package com.demo2do.boyle.backend.web.controller.advertisement;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.advertisement.AdvertisementService;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.boyle.common.entity.advertisement.PutType;
import com.demo2do.boyle.common.entity.advertisement.Status;
import com.demo2do.boyle.common.entity.customer.social.SocialType;
import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author mohanlan leo
 *
 */
@Controller
@RequestMapping("/advertisements")
@MenuSetting("menu-advertisement")
public class AdvertisementController {
	
	@Autowired
	private AdvertisementService advertisementService;
	
    /**
     * list all advertisements
     * 
     * @return the advertisement list
     */
    @RequestMapping(value= "")
    @MenuSetting("submenu-advertisement-list")
    public ModelAndView list(@RequestParam(value = "status",required = false) Status status ,Page page, HttpServletRequest request) {
    	
        ModelAndView modelAndView = new ModelAndView("advertisement/advertisement-list");
        
        int count = advertisementService.count(status);
        modelAndView.addObject("count", count);
        modelAndView.addObject("status", status);
        
        if(count > 0) {
        	List<Advertisement> advertisements = advertisementService.list(status, page);
        	modelAndView.addObject("advertisements", advertisements);
        }
        
        // handle query string
        String queryString = request.getQueryString();
        if (!StringUtils.isEmpty(queryString)) {
        	
        	Map<String, String> queries = StringUtils.parseQueryString(queryString);
            if(queries.containsKey("page")) {
                queries.remove("page");
            }

    		modelAndView.addObject("queryStatus", StringUtils.toQueryString(queries));
		}
        
        return modelAndView;
    }
    
    /**
     * 
     * display the search panel
     * @return
     */
    @RequestMapping("/search")
    public String search() {
        return "advertisement/advertisement-search";
    }
    
    /**
     * do search
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-advertisement-list")
    public ModelAndView onSearch(Page page, HttpServletRequest request,
            @RequestParam(value = "aduserName", required = false) String aduserName,
            @RequestParam(value = "adTitle", required = false) String adTitle,
            @RequestParam(value = "calType", required = false) PutType putType,
            @RequestParam(value = "socialType", required = false) SocialType socialType,
            @RequestParam(value = "title", required = false) String maTitle,
            @RequestParam(value = "createDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createDate) {

        ModelAndView modelAndView = new ModelAndView("advertisement/advertisement-list");

        Filter filter = new Filter().addLike("aduser.profile.nickname", aduserName)
                                    .addLike("adTitle", adTitle)
                                    .addEquals("type", putType)
                                    .addEquals("socialType", socialType)
                                    .addEquals("material.title", maTitle)
                                    .addEquals("createDate", createDate);
        
        int count = advertisementService.count(filter);
        modelAndView.addObject("count", count);
        
        if(count >0) {
        	List<Advertisement> advertisements = advertisementService.list(Advertisement.class, filter, page);
        	modelAndView.addObject("advertisements", advertisements);
        }
        
        // handle query string
        String queryString = request.getQueryString();
        Map<String, String> queries = StringUtils.parseQueryString(queryString);
        if(queries.containsKey("page")) {
            queries.remove("page");
        }

		modelAndView.addObject("queryString", StringUtils.toQueryString(queries));

        return modelAndView;
    }
    
    
    /**
     * view the advertisement detail
     * 
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    @MenuSetting("submenu-advertisement-list")
    public ModelAndView view(@PathVariable("id") Integer id) {
        
    	ModelAndView modelAndView = new ModelAndView("advertisement/advertisement-view");
        
        Advertisement advertisement = advertisementService.load(Advertisement.class, id);
        
        modelAndView.addObject("advertisement", advertisement);
        
        return modelAndView;
    }
    
}
